var contentDivId = '#content';

function sayHello() {
    say("Welcome to my website");
}

// ran once on the site startup
// triggers when domready
function initialize() {
    if ( $.browser.msie ) {
        // extra tag is added in the body to warn if it is IE :P
        // so that we can custom our css accordingly
        $("body").addClass("ie");
        if ( $.browser.version == "6.0" ) {
            $("body").addClass("ie6");
        }
    }
    // creating the rounded corner for the main content
    $("#wrapper").createRoundedCorner();
    // intersepting menu links to provide ajex content load
    interseptMenuLinks();
    roundedCorners();
    activateSpeech($("body"));
    //load home page
    if(document.location.hash) {
        page = document.location.hash.split("#")[1].toLowerCase();
        loadFromUrl("pages/" + page + ".html", page);
    } else
        loadFromUrl("pages/home.html", "home");
}

function roundedCorners() {
    $(".round_me").createRoundedCorner();
    $(".round_me").removeClass("round_me");
}

//runs always after ajax content load
function interceptAfterContentLoad(rootElement){
    roundedCorners();
    intersecptAjaxLinkInContent();
    activateSpeech(rootElement);
    loadRecentBlogFeed();
}

function interseptMenuLinks() {
    $(".ajaxLink", "#navigation").unbind("click").click(
        function() {
            interseptLinkForAjaxContentLoad($(this));
            return false;
        }
    );
}

function interseptLinkForAjaxContentLoad(link) {
    href = link.attr("href");
    menuId = link.attr("select");
    document.location.hash = href.match(/pages\/[a-z]+\./)[0].split("/")[1].split(".")[0];
    loadFromUrl(href, menuId);
}

function loadFromUrl(href, menuId) {
    var contentDiv = $(contentDivId);
    contentDiv.animate({
        height: "0%",
        opacity: 0
    }, 500, function() {
        contentDiv.animate({
            height: "100%",
            opacity: 1
        }).html("<div class='loading'></div>");
        $.ajax({
            url: href,
            success: function(response) {
                showContents(response);
                //now select the appropiate menu
                //alert(menuId);
                $("a", "#navigation").removeClass("selected");
                $("#mnu_" + menuId).addClass("selected");
            }
        });
    });
}

function showContents(response) {
    var contentDiv = $(contentDivId);
    contentDiv.animate({
        height: "0%",
        opacity: 0
    }, 500, function() {
        contentDiv.html(response);
        interceptAfterContentLoad(contentDiv);
    }).animate({
        height: "100%",
        opacity: 1
    }, 500);
}

function intersecptAjaxLinkInContent() {
    $(".ajaxLink", contentDivId).unbind("click").click(
        function() {
            interseptLinkForAjaxContentLoad($(this));
            return false;
        });
}

function loadRecentBlogFeed() {
    if(!$("#recent_blog")) return;
    var numberOfFeed = 3;
    var technicalBlogFeed = "http://jitu-blog.blogspot.com/feeds/posts/default?orderby=published&alt=json-in-script&max-results=" + numberOfFeed + "&callback=?";
    var distBlogFeed = "http://distash.blogspot.com/feeds/posts/default?orderby=published&alt=json-in-script&max-results=" + numberOfFeed + "&callback=?";
    loadBlogFeed("Recent Blogs", numberOfFeed, technicalBlogFeed, $("#recent_blog .content"));
    loadBlogFeed("Bogging on Distribited", numberOfFeed, distBlogFeed, $("#recent_dist_blog .content"));
}

function loadBlogFeed(title, numberOfFeed, jsonFeed, loadInContent) {
    $.getJSON(jsonFeed,
        function(data) {
            var recent_blog_contents = '';
            $.each(data.feed.entry, function(i, postentry) {
                var postTitle = postentry.title.$t;
                var postLink = '';

                $.each(postentry.link, function(i, link) {
                    postLink = link.href;
                    return;
                });
                var postTags = new Array();
                if(postentry.category) {
                    $.each(postentry.category, function(i, category) {
                        postTags.push(category.term);
                    });
                }
                postTags = postTags.length > 0 ? postTags.join(", ") : "nothing specific";
                var seperator = '';
                if(numberOfFeed != i+1) seperator = "<hr size='1'/>";

                recent_blog_contents += tmpl("blog_link_tmpl", {
                    title:postTitle,
                    link:postLink,
                    seperator:seperator,
                    tags:postTags
                });
            });
            loadInContent.html("<h3>" + title + "</h3><br/>" + recent_blog_contents);
            activateSpeech(loadInContent);
        });    
}